
Imports std

Public Class ConsoleText
    Inherits Windows.Forms.TextBox

    Protected Friend Shared n2nr As vector(Of pair(Of String, String))

    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            If Not isemptystring(value) Then
                'If s.IndexOf(newline.incode) = npos AndAlso _
                '    (s.IndexOf(character.newline) <> npos OrElse s.IndexOf(character.return) <> npos) Then
                strrplc2(value, n2nr)
                'End If

                Dim index As Int32
                Dim crlf As String = newline.incode
                index = value.IndexOf(character.return)
                While index <> npos AndAlso index < strlen(value) - 1 'do not care about the last \r, as there is no data to over-write now
                    If (value(index + 1) <> character.newline) Then
                        Dim nextCR As Int32
                        nextCR = value.IndexOf(character.return, index + 1)
                        If nextCR = npos Then
                            nextCR = strlen(value)
                        End If
                        Dim lastCRLF As Int32
                        If index > 0 Then
                            lastCRLF = value.LastIndexOf(crlf, index - 1)
                            If lastCRLF = npos Then
                                lastCRLF = 0
                            Else
                                lastCRLF += 2
                            End If
                        Else
                            lastCRLF = 0
                        End If

                        'not include cr char for length
                        Dim thislinelen As Int32 = index - lastCRLF
                        Dim nextlinelen As Int32 = nextCR - index - 1
                        Dim insertafter As String = emptyString
                        If thislinelen > nextlinelen Then
                            insertafter = strmid(value, lastCRLF + nextlinelen, thislinelen - nextlinelen)
                        End If
                        value = value.Remove(lastCRLF, thislinelen + 1)
                        nextCR -= thislinelen + 1
                        lastCRLF -= thislinelen + 1
                        If Not isemptystring(insertafter) Then
                            value = value.Insert(nextCR, insertafter)
                        End If
                        index = lastCRLF - 1
                    End If
                    index = value.IndexOf(character.return, index + 1)
                End While
            End If

            MyBase.Text = value
        End Set
    End Property

    Shared Sub New()
        n2nr = New vector(Of pair(Of String, String))
        n2nr.push_back(make_pair(newline.incode, Convert.ToString(character.null)))
        n2nr.push_back(make_pair(Convert.ToString(character.newline), Convert.ToString(character.null)))
        'n2nr.push_back(make_pair(Convert.ToString(character.return), Convert.ToString(character.null)))
        n2nr.push_back(make_pair(Convert.ToString(character.null), newline.incode))
    End Sub

End Class
